package com.test.webflux.config.router;

import com.test.webflux.entity.Product;
import com.test.webflux.entity.ResponseResult;
import com.test.webflux.service.ProductService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

/**
 * TODO
 *
 * @Description
 * @Author lingmin
 * @Date 2025/4/14 16:29
 **/
public class ProductRouter {

    /**
     * 产品路由
     * @param productService
     * @return
     */
    public RouterFunction<ServerResponse> productRoutes(ProductService productService) {
        return RouterFunctions.route()
                .path("/product", builder -> builder  // 所有 /product 下的路由
                        .GET("/title", request -> ServerResponse.ok().body(productService.getTitle() , String.class))
                        .POST("/list",   request -> {
                            return request.bodyToMono(Product.class).flatMap(product -> {
                                return ServerResponse.ok().body(productService.list(product) , ResponseResult.class);
                            });
                        })
                        .POST("/save", request -> ServerResponse.ok().body(productService.save(request.bodyToMono(Product.class)) , ResponseResult.class))
                        .POST("/subscribe", request -> ServerResponse.ok().build(productService.subscribe(request.bodyToMono(Product.class))))
                        .POST("/getProductById/{id}", request -> ServerResponse.ok().body(productService.getProductById(request) , ResponseResult.class))
                        .POST("/save2", productService::save2)
                        .POST("/exeption/{id}", productService::exeptionHandle)
                        .POST("/monoexeption/{id}", request -> ServerResponse.ok().body(productService.monoExeptionHandle(request) , ResponseResult.class))
                )
                .build();
    }
}
